<?php
/**
 * Created by PhpStorm.
 * User: mqq
 * Date: 2016/4/25
 * Time: 22:17
 */

namespace Addons\SchoolNews\Controller;

use DiDom\Document;
use Home\Controller\AddonsController;
use Org\Util\Snoopy;

class NewsListController extends AddonsController
{
    public function _initialize()
    {
        vendor("DiDom.Document");
        vendor("DiDom.Query");
        vendor("DiDom.Element");
    }

    public function index()
    {
        $href=I("get.href");
        $url="http://gxsdxy.cn/catalog-list/96/".$href;
//        $url="http://gxsdxy.cn/catalog-list/96/22?page=1";
        $sn = new Snoopy();
        $sn->fetch($url);
        $document = new Document($sn->results);
        $posts = $document->find('.listbox2 .view-content tr');
        foreach ($posts as $post) {
            $rows[] = array(
                "href" => preg_replace('/\D/s', '', $post->find('a')[0]->href),
                "title" => $post->find('a')[0]->text(),
                "date" => $post->find('.date-display-single')[0]->text()

            );
        }
//        dump($rows);
        $this->assign("list",$rows);
        $title = $document->find('.node-title .field-content')[0]->text();
        $this->assign("title",$title);
        $this->assign("href",$href);
        $this->display();
    }

    public function pullup(){
        $p=I("post.p");
        $href=I("post.href");
        $url="http://gxsdxy.cn/catalog-list/96/".$href."?page=".$p;
//        $url="http://gxsdxy.cn/catalog-list/96/22?page=1";
        $sn = new Snoopy();
        $sn->fetch($url);
        $con=$sn->results;
            $document = new Document($con);
            $posts = $document->find('.listbox2 .view-content tr');
            foreach ($posts as $post) {
                $rows[] = array(
                    "href" => preg_replace('/\D/s', '', $post->find('a')[0]->href),
                    "title" => $post->find('a')[0]->text(),
                    "date" => $post->find('.date-display-single')[0]->text()
                );
            }
            $this->ajaxReturn($rows);


    }

    private function getNewsList($href)
    {
        $url="http://gxsdxy.cn/catalog-list/96/".$href;
        $sn = new Snoopy();
        $sn->fetch($url);
        $document = new Document($sn->results);
        $posts = $document->find('.listbox2 .view-content tr');
        foreach ($posts as $post) {
            $rows[] = array(
                "href" => preg_replace('/\D/s', '', $post->find('a')[0]->href),
                "title" => $post->find('a')[0]->text(),
                "date" => $post->find('.date-display-single')[0]->text()

            );
        }
        return $rows;
    }
}